<template>
  <view class="township">
    <view v-for="(item, index) in list" :key="index" class="town-item" @click="gotoRouter(item)">
      <view class="town-item-title">{{ item.ENTERPRISENAME }}</view>
      <view class="town-item-img">
        <u-image width="100%" height="14px" src="../../static/img/task/open.png" />
      </view>
    </view>
  </view>
</template>

<script>
import { queryBodyByProduce } from '@/api/formview';
import { Message } from '@/utils/utils';
import { userIdent } from '@/utils/user.js';
import { getLocalStorage } from '@/utils/utils';
export default {
  name: 'Township',
  data() {
    return {
      list: [],
      listParams: {
        pageIndex: 0,
        pageSize: 100,
        pageType: 1,
        tableId: 391
      },
      title: ''
    };
  },
  async created() {
    this.getTitle();
    await this.getList();
    this.getRoleType();
  },
  methods: {
    getTitle() {
      const { title } = this.$route.query;
      this.title = title;
    },
    async getList() {
      const { data, success, message } = await queryBodyByProduce(this.listParams);
      if (success && data) {
        this.list = data;
      } else {
        this.list = [];
        Message(message);
      }
    },
    gotoRouter({ CENTERPRISECODE1, ENTERPRISENAME }) {
      const param = {
        query: {
          title: this.title,
          CENTERPRISECODE1,
          ENTERPRISENAME
        }
      };
      if (this.title === '项目分析') {
        param.path = '/capital/project';
      } else if (this.title === '资金监管') {
        param.path = '/capital/supervision';
      }
      this.$router.push(param);
    },
    // 判断是否为财政角色,不是就直接跳到下一页相应乡镇
    getRoleType() {
      if (!userIdent()) {
        const codeName = getLocalStorage('mIsbnCodeName');
        // 获取乡镇列表
        const findTownItem = this.list.find(item => item.ENTERPRISENAME === codeName);
        if (!findTownItem) {
          this.list = [];
          uni.showModal({
            title: '提示',
            content: '该用户未配置所属乡镇,请联系运维配置',
            success: function(res) {
              if (res.confirm) {
                uni.redirectTo({ url: '/' });
              } else if (res.cancel) {
                uni.redirectTo({ url: '/' });
              }
            }
          });
        } else {
          const { CENTERPRISECODE1, ENTERPRISENAME } = findTownItem;
          // let url;
          // if (this.title === '项目分析') {
          //   url = '/capital/project?';
          // } else if (this.title === '资金监管') {
          //   url = '/capital/supervision?';
          // }
          // url += `title=${this.title}&CENTERPRISECODE1=${CENTERPRISECODE1}&ENTERPRISENAME=${ENTERPRISENAME}`;
          // console.log(url);
          // uni.redirectTo({ url });
          const param = {
            query: {
              title: this.title,
              CENTERPRISECODE1,
              ENTERPRISENAME
            }
          };
          if (this.title === '项目分析') {
            param.path = '/capital/project';
          } else if (this.title === '资金监管') {
            param.path = '/capital/supervision';
          }
          this.$router.replace(param);
        }
      }
    }
  }
};
</script>

<style lang="less" scoped>
.township{
  background-color: #F5F7FA;
  padding-top: 12px;
  .town-item{
    height: 50px;
    background-color: #fff;
    border-bottom: 1px solid rgb(240, 240, 240);
    display: flex;
    padding: 0 12px;
    .town-item-title{
      flex: 1;
      line-height: 50px;
      font-size: 16px;
      color: #303133;
      font-family: PingFangSC, PingFangSC-Regular;
    }
    .town-item-img{
      width: 8px;
      height: 50px;
      display: flex;
      align-items: center;
    }
  }
}
</style>
